package it.fuesi.synotools.services.tasks.download;

import android.content.Context;

import it.fuesi.synotools.helpers.LayoutHelper;
import it.fuesi.synotools.model.base.VoidDataObject;
import it.fuesi.synotools.model.base.VoidResponse;
import it.fuesi.synotools.model.download.DownloadStationClientParameters;
import it.fuesi.synotools.services.cache.CacheService;
import it.fuesi.synotools.services.client.DownloadStationClient;
import it.fuesi.synotools.services.client.ServiceClientFactory;
import it.fuesi.synotools.services.tasks.base.BaseTask;

/**
 * Created by pascotto on 17/10/14.
 */
public class CreateDownloadTask extends BaseTask<VoidResponse, VoidDataObject, DownloadStationClientParameters> {

    private static final String TAG = CreateDownloadTask.class.getName();

    public CreateDownloadTask(Context context){
        super(context);
    }

    @Override
    protected String getCacheKey() {
        return null;
    }


    @Override
    public VoidResponse run(DownloadStationClientParameters params)  throws Exception{


            publishProgress("Adding new download task");
            DownloadStationClient client = ServiceClientFactory.getClient(DownloadStationClient.class);

            return client.createTask(params.getUri());

    }

    @Override
    public void onComplete(VoidResponse returnObject) {
        super.onComplete(returnObject);

        if(returnObject != null && returnObject.isSuccess()){
            CacheService.expireDownloadStationCache(getContext());
            LayoutHelper.showToast(getContext(), "Added to download queue");
        }


    }
}
